package department

import (
	"yunj/pkg/yunj/app/controller/admin"
	"yunj/pkg/yunj/app/service/admin/departmentservice"
	"yunj/pkg/yunj/core/enum/errcode"
	"yunj/pkg/yunj/core/response"
	"yunj/pkg/yunj/util"

	"github.com/gin-gonic/gin"
)

// 部门岗位管理
type Postion struct {
	admin.Controller
}

// 列表
func (ctrl *Postion) List(ctx *gin.Context) {
	tableBuilder, err := departmentservice.PostionPage.ListBuilder(ctx)
	if err != nil {
		if util.IsAsync(ctx) {
			response.Fail(ctx, err.Error())
		} else {
			util.RedirectAdminTips(ctx, errcode.ERROR, err.Error())
		}
		return
	}
	ctrl.RenderTable(ctx, tableBuilder)
}

// 添加
func (ctrl *Postion) Add(ctx *gin.Context) {
	formBuilder, err := departmentservice.PostionPage.FormBuilder(ctx, false)
	if err != nil {
		if util.IsAsync(ctx) {
			response.Fail(ctx, err.Error())
		} else {
			util.RedirectAdminTips(ctx, errcode.ERROR, err.Error())
		}
		return
	}
	ctrl.RenderForm(ctx, formBuilder)
}

// 编辑
func (ctrl *Postion) Edit(ctx *gin.Context) {
	formBuilder, err := departmentservice.PostionPage.FormBuilder(ctx, true)
	if err != nil {
		if util.IsAsync(ctx) {
			response.Fail(ctx, err.Error())
		} else {
			util.RedirectAdminTips(ctx, errcode.ERROR, err.Error())
		}
		return
	}
	ctrl.RenderForm(ctx, formBuilder)
}

// 岗位下拉搜索可选项
func (ctrl *Postion) PostionDropdownSearchOptions(ctx *gin.Context) {
	options, err := departmentservice.PostionPage.PostionDropdownSearchOptions(ctx)
	if err != nil {
		response.Fail(ctx, err.Error())
	} else {
		response.Success(ctx, options)
	}
}
